package com.rjt.douyin.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.rjt.common.util.JsonUtil;
import com.rjt.common.util.TextUtil;
import com.rjt.douyin.vo.Douyin;

import blade.kit.http.HttpRequest;
@Controller
@RequestMapping("douyin")
public class DouyinController {
	
	@GetMapping("jx")
	@ResponseBody
	public Douyin jx(String url,HttpServletRequest req,HttpServletResponse resp) throws IOException {
		 if(url.indexOf("v.douyin")!=-1) {
			 return parseUrl(url);
		 }
		 return null;
	}
	@GetMapping("index")
	public String index() {
		return  "douyin";
	}
	
	public static void main(String[] args) {
		DouyinController a=new DouyinController();
		a.parseUrl("http://v.douyin.com/r2w3sN/");
	}
	/**
	 * @param string
	 */
	private  Douyin parseUrl(String url) {
		// TODO Auto-generated method stub
		HttpRequest request = HttpRequest.get(url);;
		String res = request.body();
		String mid=TextUtil.getSubString(res, "https://www.iesdouyin.com/share/video/", "/?");
		
		url ="https://api-hl.amemv.com/aweme/v1/aweme/detail/?retry_type=no_retry&i"
				+ "id=43619087057&device_id=57318346369&ac=wifi&channel=update&aid=1128&app_name=aweme&"
				+ "version_code=251&version_name=2.5.1&device_platform=android&ssmix=a&device_type=MI+"
				+ "8&device_brand=xiaomi&language=zh&os_api=22&os_version=5.1.1&uuid=865166029463703&o"
				+ "penudid=ec6d541a2f7350cd&manifest_version_code=251&resolution=1080*1920&dpi=480&upd"
				+ "ate_version_code=2512&_rticket=1559206461097&ts=1559206460&as=a115996edcf39c7adf4355"
				+ "&cp=9038c058c7f6e4ace1IcQg&mas=01af833c02eb8913ecc7909389749e6d89acaccc2c662686ecc69c&aweme_id="
				+mid;
		request=HttpRequest.get(url).header("User-Agent","okhttp/3.10.0.1");
				
		res = request.body();
		
	  	Douyin douyin = new Douyin();
	    String title = JsonUtil.getJsonValue(res, "aweme_detail.share_info.share_title");
	    String playAddr = JsonUtil.getJsonValue(res, "aweme_detail.video.play_addr.url_list[0]");
		douyin.setTitle(title);
		douyin.setUrl(playAddr);
		return douyin;
		
	}
	
}
